#include "TcpServer.hpp"
#include "Protocol.hpp"
#include <memory>

using namespace tcp_server_ns;
using namespace protocol_ns;

Response Calculate(const Request &req)
{
    Response resp;
    switch (req._op)
    {
    case '+':
        resp._result = req._x + req._y;
        resp._code = 0;
        break;
    case '-':
        resp._result = req._x - req._y;
        resp._code = 0;
        break;
    case '*':
        resp._result = req._x * req._y;
        resp._code = 0;
        break;
    case '/':
        if(req._y == 0)
        {
            resp._code = 1;
            resp._result = 0;
        }
        else
        {
            resp._result = req._x / req._y;
            resp._code = 0;
        }
        break;
    default:
        resp._code = 2;
        break;
    }

    return resp;
}

int main(int argc, char* argv[])
{
    uint16_t port = atoi(argv[1]);

    std::unique_ptr<tcpServer> server(new tcpServer(Calculate, port));

    server->InitServer();
    server->Start();

    return 0;
}